﻿using AIStudio.Core;
using CommonServiceLocator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace AIStudio.Wpf.BasePage.ControlHelpers
{
    public class ThemeChangedHelper
    {
        #region ThemeChangedHelper

        private static HashSet<DependencyObject> AcceptThemeObject = new HashSet<DependencyObject>();
        private static ILocalSetting _localSetting;

        public static void SetAcceptThemeChanged(DependencyObject element, bool value)
        {
            element.SetValue(AcceptThemeChangedProperty, value);
        }

        public static bool GetAcceptThemeChanged(DependencyObject element)
        {
            return (bool)element.GetValue(AcceptThemeChangedProperty);
        }

        public static readonly DependencyProperty AcceptThemeChangedProperty =
            DependencyProperty.RegisterAttached("AcceptThemeChanged", typeof(bool), typeof(ThemeChangedHelper), new PropertyMetadata(false, OnAcceptThemeChanged));
        protected static void OnAcceptThemeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (_localSetting == null)
            {
                _localSetting = ServiceLocator.Current.GetInstance<ILocalSetting>();
                _localSetting.AccentChanged += AccentChanged;
            }

            if ((bool)e.NewValue == true)
            {
                AcceptThemeObject.Add(sender);
            }
            else
            {
                AcceptThemeObject.Remove(sender);
            }
        }

        private static void AccentChanged()
        {
            foreach(var obj in AcceptThemeObject)
            {
                if (obj.ContainsProperty("IsThemeChanged"))
                {
                    obj.SetPropertyValue("IsThemeChanged", false);
                    obj.SetPropertyValue("IsThemeChanged", true);
                }
            }
        }

      
        #endregion


    }
}
